<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\user\model\UserModel;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{
     

    function _initialize()
    {
        parent::_initialize();

        $province = Db::name('tree')->where ( array('pid'=>1) )->select ();
        $this->assign('province',$province);

    }

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $where   = '';
        $request = input('request.');

        if (!empty($request['uid'])) {
            $id = intval($request['uid']);
            $where .= " and id = '$id' ";
        }
        $keywordComplex = [];
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $keywordComplex['user_login|user_nickname|user_email|mobile']    = ['like', "%$keyword%"];
        }
        $usersQuery = Db::name('user');

        $list = $usersQuery->whereOr($keywordComplex)->where('user_type = 2'.$where)->order("create_time DESC")->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功！", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }

    // 添加前台用户
    public function add(){

        return $this->fetch();
    }

    public function getRegion(){

        $Region=Db::name("tree");
        $map['pid']=$_REQUEST["pid"];
        $map['type']=$_REQUEST["type"];
        $list=$Region->where($map)->select();
        echo json_encode($list);
    }

    // 添加操作
    public function addpost(){
// print_r($_POST);exit;
        if($this->request->isPost()){

            $data = $this->request->param();

            $result = $this->validate($data,'AdminIndex');

            if($result!== true){
                $this->error($result);
            }else{
                    $data['user_pass'] = cmf_password($data['user_pass']);
                    $data['birthday']  = strtotime($data['birthday']);
                    $data['user_type'] = 2;
                    $data['create_time'] = time();

                    if($this->request->param('face_card')){
                        $data['face_card'] = 'upload/'.$data['face_card'];
                    }
                    if($this->request->param('back_card')){
                        $data['back_card'] = 'upload/'.$data['back_card'];
                    }
                    if($this->request->param('avatar')){
                        $data['avatar']    = 'upload/'.$data['avatar'];
                    }
                    
                    $data['last_login_ip'] = get_client_ip(0,true);

                    $user = new UserModel();

                    $result = $user->allowField(true)->save($data); 

                    if($result !== false){

                            $this->success('添加成功！',url('admin_index/index'));
                    }else{
                            $this->error('添加失败！');
                    }
            }

        }
    }

    // 编辑
    public function edit(){

        $id = $this->request->param('id');

        $user = new UserModel();

        $data = Db::name('user')->where(array('id'=>$id,'user_type'=>2))->find();

        $city_list = Db::name('tree')->where(array('pid'=>$data['province']))->select();

        $town_list = Db::name('tree')->where(array('pid'=>$data['city']))->select();

        $this->assign('city_list',$city_list);

        $this->assign('town_list',$town_list);

        $this->assign('data',$data);

        $this->assign('id',$id);

        return $this->fetch();
    }


    // 编辑处理操作
    public function editpost(){

        if($this->request->isPost()){

                $data = $this->request->param();

                $value = [];

                $result = $this->validate($data,'AdminIndex.edit');

                if($result!== true){
                    $this->error($result);
                }else{

                    if($this->request->param('user_pass')){
                        $value['user_pass']   = cmf_password($data['user_pass']);
                    }
                    if($this->request->param('face_card')){
                        $value['face_card'] = 'upload/'.$data['face_card'];
                    }
                    if($this->request->param('back_card')){
                        $value['back_card'] = 'upload/'.$data['back_card'];
                    }
                    if($this->request->param('avatar')){
                        $value['avatar']    = 'upload/'.$data['avatar'];
                    }

                    $value['user_nickname'] = $data['user_nickname'];
                    $value['user_email']    = $data['user_email'];
                    $value['user_name']     = $data['user_name'];
                    $value['sex']           = $data['sex'];
                    $value['province']      = $data['province'];
                    $value['city']          = $data['city'];
                    $value['town']          = $data['town'];
                    $value['id_card']       = $data['id_card'];
                    $value['salary']        = $data['salary'];
                    $value['work_year']     = $data['work_year'];
                    $value['designer_level']= $data['designer_level'];
                    $value['signature']     = trim($data['signature']);
                    $value['birthday']      = strtotime($data['birthday']);
                    $value['user_type']     = 2;
                    $value['update_time']   = time();
                    $value['last_login_ip'] = get_client_ip(0,true);

                    $id = $this->request->param('id');
                   
                    $result = Db::name('user')->where('id',$id)->update($value); 
                    // var_dump($result);
                    // print("<br/>");
                    // print_r($result);exit;
                    if($result){

                            $this->success('保存成功！',url('admin_index/edit',array('id'=>$id)));
                    }else{
                            $this->error('保存失败！');
                    }

                }

            
        }

    }











}
